var rootPath = getRootPath();

var initGearboxTypeData = {};



$(document).ready(function(){
	var seriesCode = getQueryParamValue("seriesCode");

	var brandCode = getQueryParamValue("brandCode");

	//初始化加载该车系下车型数据
	initModeData(seriesCode);
	
	loadPackageList(seriesCode);
});


//根据选择的排量和变速箱类型加载显示列表
function loadPackageList(seriesCode){
	var currentSeelctDisplacement = $("#currentSeelctDisplacement").val();
	var currentSeelctGearboxType = $("#currentSeelctGearboxType").val();
	var packageHtml = '';
	$("#modelExhibitionGroupContent").empty();
	
	
	$.ajax({
		method : 'get',
		url : rootPath+'/wechat/api/showroom/packages/group/displacementAndGearboxType?seriesCode='+seriesCode+"&displacement="+currentSeelctDisplacement+"&gearboxType="+currentSeelctGearboxType,
		async:false,
		error:function(XMLHttpRequest, ajaxOptions, thrownError){
			alert("连接服务器错误!");
		},
		success : function(resultData){
			if(resultData==undefined || resultData==''){
				return;
			}
			
			$.each(resultData,function(packageIndex,packageObj){
				packageHtml += '<div class="container">'
					 +'	<h5><a class="selectModel" href="packageExhibition.html?packageCode='+packageObj.packageCode+'">'+packageObj.packageName+'<span class="glyphicon glyphicon-chevron-right pull-right"></span></a></h5>'		        
				     +'      <div class="row">'
				     +'        <div class="col-xs-6 col-sm-6 col-md-6">'
				     +'           <p><span class="text-muted">指导价：'+emptyValConvert(packageObj.guidePrice)+'</span></p>'
				     +'           <p><span class="text-muted"><font color="red">本店价：'+emptyValConvert(packageObj.localPrice)+'</font></span></p>'
				     +'        </div>'
				     +'        <div class="col-xs-3 col-sm-3 col-md-3" style="margin-top: 25px;">'
					 +'			  <button onclick="modelExhibitionTestdriveAppointFun(this)" packageCode='+packageObj.packageCode+' type="button" class="btn btn-warning btn-sm">试驾预约</button>'
		             +'        </div>'
				     +'        <div class="col-xs-3 col-sm-3 col-md-3" style="margin-top: 25px;">'
					 +'			  <button onclick="modelExhibitionAskingPricetFun(this)" packageCode='+packageObj.packageCode+' type="button" class="btn btn-success btn-sm"  style="background-color: #FC5965;border-color:#FC5965">咨询底价</button>'		            
		             +'        </div>'
				     +'     </div>'
					 +'   <hr></hr>'
	                 +'</div>';
			});
			
			$("#modelExhibitionGroupContent").append(packageHtml);
		}
	});


}


//选择排量
function displacementSelect(obj){
	$(obj).parent().siblings().each(function(){
		$(this).removeClass("pitchOn");
	});
	
	$(obj).parent().addClass("pitchOn");
	$("#currentSeelctDisplacement").val($(obj).attr("displacement"));
	var seriesCode = $(obj).attr("seriesCode");

	loadPackageList(seriesCode);
}

//选择变速箱类型
function gearboxTypeSelect(obj){
	$(obj).parent().siblings().each(function(){
		$(this).removeClass("pitchOn");
	});
	
	$(obj).parent().addClass("pitchOn");
	
	$("#currentSeelctGearboxType").val($(obj).attr("gearboxType"));
	
	var seriesCode = $(obj).attr("seriesCode");
	
	loadPackageList(seriesCode);

}

function initModeData(seriesCode){
	$("#displacementDiv").empty();
	$("#gearboxTypeDiv").empty();
	var displacementDivHtml = '<li class="pitchOn"><a seriesCode='+seriesCode+' displacement="all" onclick=displacementSelect(this) href="#"><span>全部排量<span></a></li>';
	var gearboxTypeDivHtml = '<li class="pitchOn"><a seriesCode='+seriesCode+' gearboxType="all" onclick=gearboxTypeSelect(this) href="#"><span>全部变速箱</span></a></li>';
	$.ajax({
		method : 'get',
		url : rootPath+'/wechat/api/showroom/series/group/displacement?seriesCode='+seriesCode,
		async:false,
		error:function(XMLHttpRequest, ajaxOptions, thrownError){
			alert("连接服务器错误!");
		},
		success : function(resultData){
			if(resultData==undefined || resultData==''){
				return;
			}
			$("#seriesImage").attr("src",rootPath+'/images/'+resultData.brandName+'/外观/'+resultData.seriesName+'/'+resultData.seriesName+'.jpg');
			
			$("#seriesNameSpan").html(resultData.seriesName);
			var localPriceMinTemp = resultData.localPriceMin;
			var localPriceMaxTemp = resultData.localPriceMax;
			if(localPriceMinTemp!=undefined && localPriceMinTemp!=''){
				$("#localPriceMinSpan").html(localPriceMinTemp);
			}
			if(localPriceMaxTemp!=undefined && localPriceMaxTemp!=''){
				$("#localPriceMaxSpan").html(localPriceMaxTemp);
			}
			
			
			$.each(resultData.models,function(modelIndex,modelObj){
				displacementDivHtml += '<li><a seriesCode='+seriesCode+' displacement = '+modelObj.displacement+' onclick=displacementSelect(this) href="#"><span>'+modelObj.displacement+'</span></a></li>';
			});
			
			$("#displacementDiv").append(displacementDivHtml);

		}
	});
	
	$.ajax({
		method : 'get',
		url : rootPath+'/wechat/api/showroom/series/group/gearboxType?seriesCode='+seriesCode,
		async:false,
		error:function(XMLHttpRequest, ajaxOptions, thrownError){
			alert("连接服务器错误!");
		},
		success : function(resultData){
			if(resultData==undefined || resultData==''){
				return;
			}
			
			$.each(resultData.models,function(modelIndex,modelObj){
				gearboxTypeDivHtml += '<li><a seriesCode='+seriesCode+' gearboxType='+modelObj.gearboxType+' onclick=gearboxTypeSelect(this) href="#"><span>'+modelObj.gearboxType+'</span></a></li>';
			});
			
			$("#gearboxTypeDiv").append(gearboxTypeDivHtml);
			
			$("#seriesSelectExhibitionDiv").removeClass("active in");
		    $("#seriesExhibitionDiv").addClass("active in");

		}
	});
	
}

/*function initModeData(seriesCode){
	
	var rootPath = getRootPath();

	
	$("#modelExhibitionTab").empty();
	$("#modelExhibitionContent").empty();

	
	$.ajax({
		method : 'get',
		url : rootPath+'/wechat/api/showroom/series/group/displacement?seriesCode='+seriesCode,
		error:function(XMLHttpRequest, ajaxOptions, thrownError){
			alert("连接服务器错误!");
		},
		success : function(resultData){
						
			$("#seriesImage").attr("src",rootPath+'/images/'+resultData.brandName+'/外观/'+resultData.seriesName+'/'+resultData.seriesName+'.jpg');
			
			$("#seriesNameSpan").html(resultData.seriesName);
			
			var localPriceMinTemp = resultData.localPriceMin;
			var localPriceMaxTemp = resultData.localPriceMax;
			if(localPriceMinTemp!=undefined && localPriceMinTemp!=''){
				$("#localPriceMinSpan").html(localPriceMinTemp);
			}
			if(localPriceMaxTemp!=undefined && localPriceMaxTemp!=''){
				$("#localPriceMaxSpan").html(localPriceMaxTemp);
			}
			
			var wholeModelHtml = '';
			var wholePackageHtml = '';
			
			//全部排量
			var wholeModelHtmlAll = '<li class="active"><a href="#displacementAll" data-toggle="tab">全部排量</a></li>';
            var wholePackageHtmlAll = '<div class="tab-pane fade in active" id="displacementAll">';


			$.each(resultData.models,function(modelIndex,modelObj){
				
				var displacement = modelObj.displacement;
				var displacementReplace = modelObj.displacement.replace('.','_');
				
				var modelHtml = '<li><a href="#'+displacementReplace+'" data-toggle="tab">'+displacement+'</a></li>';
//				if(modelIndex==0){
//					modelHtml = '<li class="active"><a href="#'+displacementReplace+'" data-toggle="tab">'+displacement+'</a></li>';
//				}
				wholeModelHtml = wholeModelHtml+modelHtml;
				
				var packageHtml = '<div class="tab-pane fade" id="'+displacementReplace+'">';
//				if(modelIndex==0){
//					packageHtml = '<div class="tab-pane fade in active" id="'+displacementReplace+'">'
//					
//				}
				
				
				$.each(modelObj.packages,function(packageIndex,packageObj){		
					packageHtml += '<div class="container">'
						 +'	<h5><a class="selectModel" href="packageExhibition.html?packageCode='+packageObj.packageCode+'">'+packageObj.packageName+'<span class="glyphicon glyphicon-chevron-right pull-right"></span></a></h5>'		        
					     +'      <div class="row">'
					     +'        <div class="col-xs-6 col-sm-6 col-md-6">'
					     +'           <p><span class="text-muted">指导价：'+emptyValConvert(packageObj.guidePrice)+'</span></p>'
					     +'           <p><span class="text-muted"><font color="red">本店价：'+emptyValConvert(packageObj.localPrice)+'</font></span></p>'
					     +'        </div>'
					     +'        <div class="col-xs-3 col-sm-3 col-md-3" style="margin-top: 25px;">'
						 +'			  <button onclick="modelExhibitionTestdriveAppointFun(this)" packageCode='+packageObj.packageCode+' type="button" class="btn btn-warning btn-sm">试驾预约</button>'
			             +'        </div>'
					     +'        <div class="col-xs-3 col-sm-3 col-md-3" style="margin-top: 25px;">'
						 +'			  <button onclick="modelExhibitionAskingPricetFun(this)" packageCode='+packageObj.packageCode+' type="button" class="btn btn-success btn-sm"  style="background-color: #FC5965;border-color:#FC5965">咨询底价</button>'		            
			             +'        </div>'
					     +'     </div>'
						 +'   <hr></hr>'
		                 +'</div>';
					
					wholePackageHtmlAll += '<div class="container">'
						 +'	<h5><a class="selectModel" href="packageExhibition.html?packageCode='+packageObj.packageCode+'">'+packageObj.packageName+'<span class="glyphicon glyphicon-chevron-right pull-right"></span></a></h5>'		        
					     +'      <div class="row">'
					     +'        <div class="col-xs-6 col-sm-6 col-md-6">'
					     +'           <p><span class="text-muted">指导价：'+emptyValConvert(packageObj.guidePrice)+'</span></p>'
					     +'           <p><span class="text-muted"><font color="red">本店价：'+emptyValConvert(packageObj.localPrice)+'</font></span></p>'
					     +'        </div>'
					     +'        <div class="col-xs-3 col-sm-3 col-md-3" style="margin-top: 25px;">'
						 +'			  <button onclick="modelExhibitionTestdriveAppointFun(this)" packageCode='+packageObj.packageCode+' type="button" class="btn btn-warning btn-sm">试驾预约</button>'
			             +'        </div>'
					     +'        <div class="col-xs-3 col-sm-3 col-md-3" style="margin-top: 25px;">'
						 +'			  <button onclick="modelExhibitionAskingPricetFun(this)" packageCode='+packageObj.packageCode+' type="button" class="btn btn-success btn-sm"  style="background-color: #FC5965;border-color:#FC5965">咨询底价</button>'		            
			             +'        </div>'
					     +'     </div>'
						 +'   <hr></hr>'
		                 +'</div>';
					
						
				});
				
				packageHtml = packageHtml +'</div>';
				
				         
				wholePackageHtml = wholePackageHtml + packageHtml;
				
			});
			
			
            wholePackageHtmlAll = wholePackageHtmlAll+'</div>';
			$("#modelExhibitionTab").append(wholeModelHtmlAll+wholeModelHtml);
			
			
			$("#modelExhibitionContent").append(wholePackageHtmlAll+wholePackageHtml);
			
			
			
			$("#seriesSelectExhibitionDiv").removeClass("active in");
		    $("#seriesExhibitionDiv").addClass("active in");


		}
	});
	
}*/


function modelExhibitionTestdriveAppointFun(obj){
	var packageCode = $(obj).attr("packageCode");
	window.location.href = rootPath +"/views/at.html?packageCode="+packageCode;
}

function modelExhibitionAskingPricetFun(obj){
	var packageCode = $(obj).attr("packageCode");
	window.location.href = rootPath +"/views/ap.html?packageCode="+packageCode;
}

//新车展厅选择该品牌其他车系 
function seriesExhibitionSelect(){
	
	var rootPath = getRootPath();	
	var brandCode = getQueryParamValue("brandCode");

	$("#seriesSelectExhibitionDiv").empty();
	$.ajax({
		method : 'get',
		url : rootPath+'/wechat/api/showroom/brands?brandCode='+brandCode,
		error:function(XMLHttpRequest, ajaxOptions, thrownError){
			alert("连接服务器错误!");
		},
		success : function(resultData){
			var wholeHtml = '';
			
			
			wholeHtml +='<h4>'+resultData[0].brandName+'</h4>';
			
			wholeHtml +='<ul id="seriesSelectExhibitionDiv_listGroup" class="list-group">';
		     
			$.each(resultData[0].series,function(seriesIndex,seriesObj){					
				wholeHtml += '<a href="#" seriesCode='+seriesObj.seriesCode+' onclick="selectSeriesLoadModelData(this)" class="list-group-item">'+
								  '<span class="glyphicon glyphicon-chevron-right form-control-feedback"></span>'+
								  seriesObj.seriesName+
				            '</a>';
				
			});
			
			wholeHtml +='</ul>';
			
			
	    	$("#seriesSelectExhibitionDiv").append(wholeHtml)

	    	$("#seriesExhibitionDiv").removeClass("active in");
	        $("#seriesSelectExhibitionDiv").addClass("active in");
			
		}
		
		
	});
   	  
}

//重新选择车系  -加载车型 
function  selectSeriesLoadModelData(obj){
	
	var seriesCode = $(obj).attr("seriesCode");
	$("#currentSeelctDisplacement").val('all');
	$("#currentSeelctGearboxType").val('all');
	initModeData(seriesCode);
	loadPackageList(seriesCode);
}